﻿using ETA.Dtos;
using ETA.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Providers.Entities;
using System.Web.Security;

namespace ETA.Web.App_Start
{
    /// <summary>
    /// 实习生权限验证过滤器
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class AuthFilter:ActionFilterAttribute
    {
        private string _Roler { set; get; }
        public AuthFilter(string roler)
        {
            _Roler = roler;
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext != null)
            {
                var currentUser = BaseController.GetCurrentUser();
                var url = FormsAuthentication.LoginUrl;

                if (currentUser == null || (!string.IsNullOrEmpty(_Roler)&&currentUser.RolerName != _Roler))
                {
                    filterContext.Result = new RedirectResult(url);//权限不够的话则跳转至首页
                }
            }
        }
    }
}